<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/back/css/base.css" rel="stylesheet" type="text/css" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
</head>
<body>
	[#if contents ?? && maxlength!=0  ]
	<div class="panel panel-info panel-content" style="overflow: auto;" >
		<div class="panel-heading" >
		<table >
			<tr>	
				名称: ${enquiry.title}
			</tr>
			<tr>
				<td class="w80 ">
					<button type="button" class="btn btn-success"  onclick="submodel()" >确定</button>
				</td>
				<td class="w80 ">
					<button type="button" class="btn  btn-info"  onclick="shownocontent()" >查看未匹配行</button>
				</td>
				<td>
				 [#if titlePerfect]&nbsp;&nbsp;&nbsp;&nbsp;<b id="icon_140"  class="glyphicon glyphicon-thumbs-up">&nbsp;&nbsp;&nbsp;&nbsp;</b>[/#if]
				 [#if contentPerfect ]&nbsp;&nbsp;&nbsp;&nbsp;<b id="icon_140"  class="glyphicon glyphicon-thumbs-up">&nbsp;&nbsp;&nbsp;&nbsp;</b>[/#if]
				</td>
			</tr>
		</table>
	</div>
	<form id="itemform"  action="/purchase/o_saveitem2.do" method="post"   >
		<input type="hidden" value="${enquiry.id}" name="enquiryid" />
		<div >
		<table class="table table-hover table-bordered table-striped table-condensed">
			<thead>
				<tr>
					<th>#</th>
					<th>系统名称</th>
					[#assign  index = maxlength /]
					
					[#list 1..index  as b]
					<th class="cell_${b}">
						<select class="typename" name="typename" attnum=${b} >
							[#--标题匹配--]
							[#if titleindex?? && (titleindex?size > b-1) ]
									[#assign a = titleindex[b-1] ]
							[#else]									
									[#assign a = 0 ]
							[/#if ]
							
							<option  value="0"  >请选择</option>
							<option  [#if a==1] selected="selected" [/#if] value="1"  >设备名称</option>
							<option  [#if a==2] selected="selected" [/#if] value="2" >品牌</option>
							<option  [#if a==3] selected="selected" [/#if] value="3" >型号</option>
							<option  [#if a==4] selected="selected" [/#if] value="4" >单位</option>
							<option  [#if a==5] selected="selected" [/#if] value="5" >数量</option>
							<option  [#if a==6] selected="selected" [/#if] value="6" >技术参数</option>
						</select>
						
					</th>
					[/#list]
				</tr>
				<tr>
					<td>
					</td>
					<td>
					<span style="font-size: 10px;color: #918C8C;" >原始名称 ：</span>
					</td>
					[#list 1..index  as a]
					<td  class="cell_${a}">
						<button  onclick="delcell(${a})" class="btn btn-xs btn-danger" >删除该列</button>
						[#--原始列名--]
						[#if titles?? && (titles?size > a-1) ]
							<span style="font-size: 10px;color: #918C8C;" >${titles[a-1]}</span>
							<input  type="hidden"   name="oldtitle"  value="${titles[a-1]}"    />
						[/#if ]
					</td>	
					[/#list]
				</tr>
			</thead>
			[#assign  index = 0 /]
				[#list contents as cc ]
					<tr>
						<td>${index+1}
						<input type="hidden"  name="item[${index}].seriesnumber" value="${enquiry.seriesnumber!}"  />
						<input type="hidden"  name="item[${index}].enquiry.id" value="${enquiry.id!}"  />
						<input type="hidden"  class="fcatename"  name="item[${index}].fcatename" value="${enquiry.fcatename!}"  />
						<input type="hidden"  name="item[${index}].processid" value="${enquiry.processes[0].id!}"  />
						</td>

						[#list cc as c]
							[#if c_index==0]
							[#--第一列做为系统名称--]
								<td>
								<select class="system_item" name="item[${index}].fcateid"  >
								[#list pcats as cat]
									<option  value=${cat.id}  [#if cc[0]=="" && cat.id == enquiry.fcateid  ]selected="selected" [#elseif cat.id?string == cc[0] ]selected="selected"  [/#if]  >${cat.name}</option>							
								[/#list]
								</select>
								[#--无法识别的系统，需要保存到数据库--]
								<input type="hidden" name="item[${index}].needrepository"   value=[#if cc[0]==""]1[#else]0[/#if]  >
								</td>
							[#else]	
								<td class="cell_${c_index}"><input   name="item[${index}]"    class="input_${c_index}" type="text"  value="${c!}" > </td>
							[/#if]
						[/#list]
						[#if cc?size-1 < maxlength]
							[#--补充空白列--]
							[#list 0..(maxlength - cc?size) as c]
								<td class="cell_${c_index + cc?size}"><input   name="item[${index}]"    class="input_${c_index+ cc?size}" type="text"  value="" > </td>
							[/#list]
						[/#if]
						
					</tr>
					[#assign  index = index+1 /]
				[/#list]

			
		</table>
		</div>
		</form>
	</div>
	
	<div style="display: none;" >
		<div id="nocontent">
		<div style="overflow: scroll;height: 500px;">
			[#if nocontents?? ]
			<table >
			[#list nocontents  as nocontent]
				<tr>
					[#list nocontent  as c]
						[#if c!=""]
							<td  style="border-bottom: 1px solid black;" > ${c!}</td> 
						[/#if]	
					[/#list]
				</tr>
			[/#list]	
			</table>
			[#else]
				没有
			[/#if]
			</div>
		</div>
		
	</div>
	
	<style>
	.noselect{
		background-color: rgba(99, 230, 99, 0.8);
	}
	</style>
	<script type="text/javascript">
		//设备名称，品牌 ，型号  ,单位 ,数量,技术参数
		var selectnames =["productname","industry","model","unit","count","descr"];
	    //切换选项，确保不重复就可
		$(".typename").on("change",function(){
			
			var  thisval = $(this).val();
			
			if(thisval!=0){//将相同的选项设置为请选择
				var hadvnum =[];
				hadvnum.push(thisval);
				
				$(".typename").each(function(i){
					var otherv = $(this).val();
					if(otherv == thisval ){
						$(this).val(0);
					}else{
						hadvnum.push(otherv);
					}
				});
				
				//没有被选择的加颜色
 				var nums = hadvnum.join("");
				$(".noselect").removeClass("noselect");
				for(var i=1;i<7;i++){
					if(nums.indexOf(i)==-1){
						$(".typename").find("option[value='"+i+"']").addClass("noselect");
					}
				} 
				
			}
			
			$(this).val(thisval);
		})
		
		
		//切换系统
		$(".system_item").on("change",function(){
			var vv = $(this).val();
			$(".system_item").each(function(){
				$(this).val(vv);
			})
			var vname = $(this).find("option:selected").text();
			$(".fcatename").val(vname);
		});
		
		
		
		//准备提交
		function submodel(){
			var success = true;
			var msg ="";
			$(".typename").each(function(){
				var thisval = $(this).val();
				if(thisval==0){
					success = false;
					msg="失败：请确定每一列的名称";
					return ;
				}
				
			})
			
			if(!success){
				art.dialog.tips(msg);
				return ;
			}else{
				//确定每列的name
				$(".typename").each(function(i){
					var thisval = $(this).val();
					var attnum = $(this).attr("attnum");
					//拿到name值
					var namev = selectnames[thisval-1];
					//设置name值
					$(".input_"+attnum).each(function(){
						var oname = $(this).attr("name");
						var newname ="";
						if(oname.indexOf(".")!=-1){
							newname = oname.split(".")[0] +"."+namev;
						}else{
							newname = oname+"."+namev;
						}
						$(this).attr("name",newname);
					})
					
				})
				
				//验证数值 ,数量必须全为数字
				$("#itemform").find("input[name$='count']").each(function(){
					var num = $(this).val();
					if(isNaN(num)){
						success = false;
						return ;
					}
				})
				
				if(!success){
					art.dialog.tips("数量对应的列必须全为数字");
					return ;
				}
				
				//验证设备名称
				$("#itemform").find("input[name$='productname']").each(function(){
					var productname = $(this).val();
					if(productname==""){
						success = false;
						return ;
					}
				})
				
				if(!success){
					art.dialog.tips("设备名称不能有空！");
					return ;
				}
				
				//单位不能为空
				$("#itemform").find("input[name$='unit']").each(function(){
					var unit = $(this).val();
					if(unit==""){
						success = false;
						return ;
					}
				})
				
				if(!success){
					art.dialog.tips("单位不能有空！");
					return ;
				}
				
				
				
 				//设置后提交
  				art.dialog.post({
					url:"/purchase/o_saveitem.do",
					data:$("#itemform").serialize(),
					success:function(data){
						if(data.success){
							art.dialog.alert("操作成功",function(){
								top.closeTempTab(true);
							})
						}else{
							art.dialog.tips(data.msg);
						}
					}
				});   
				
			}
			
		}
	    function delcell(cellnum){
	    	$(".cell_"+cellnum).remove();
	    }
	    
	    
	    //改下样式
	    $(".panel-heading").width($(".table").width());
	    
	    //window.top.closeWebSocket();
	    
	    
	    //展示未匹配的
	    function shownocontent(){
	    	art.dialog({
	    		title:"未匹配",
	    	    content: document.getElementById('nocontent')
	    	});
	    }
	    
	</script>
	
	[#else]
		${msg!"没有任何信息"}
	[/#if]
</body>
</html>	
